// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

// ***************************************************************
// - [#] indicates a test step (e.g. # Go to a page)
// - [*] indicates an assertion (e.g. * Check the title)
// - Use element ID when selecting an element. Create one if none.
// ***************************************************************

// Stage: @prod
// Group: @channels @search @not_cloud

import * as TIMEOUTS from '../../../fixtures/timeouts';

describe('Search', () => {
    before(() => {
        cy.shouldNotRunOnCloudEdition();

        cy.apiUpdateConfig({
            ServiceSettings: {
                EnableTesting: true,
            },
        });
    });

    beforeEach(() => {
        cy.apiAdminLogin();
        cy.apiInitSetup({loginAfter: true}).then(({offTopicUrl}) => {
            cy.visit(offTopicUrl);
        });
    });

    it('MM-T350 - Searching displays results in RHS', () => {
        const testSearch = '/test url test-search';

        // # Post a message
        cy.postMessage(testSearch);

        cy.uiGetSearchContainer().click();

        // # Search the world 'hello' that is generated by /test command
        cy.uiGetSearchBox().type('hello{enter}').wait(TIMEOUTS.HALF_SEC);
        cy.get('#search-items-container span').should('contain.text', 'hello');

        // # RHS should be visible with search results
        cy.get('#search-items-container').should('be.visible');

        // * Verify different display formatting of the search results
        cy.get('[data-testid="search-item-container"]').first().then(($result) => {
            cy.wrap($result).contains('Basic word search: Hello world!');
            cy.wrap($result).get('h5.markdown__heading').contains('Hello');
            cy.wrap($result).get('.post-code.post-code--wrap code').contains('Hello');
            cy.wrap($result).contains('#hello');

            // # Jump to conversation
            cy.wrap($result).get('a.search-item__jump').first().click();

            cy.uiGetSearchContainer().click();

            // # Hover search query clear icon
            cy.get('.input-clear-x').first().trigger('mouseover', {force: true}).then(($span) => {
                // # Click the clear query icon
                cy.wrap($span).click({force: true});

                // * Assert search results are intact
                cy.get('[data-testid="search-item-container"]').should('be.visible');
            });
        });
    });

    it('MM-T2286 - Clicking a hashtag from a message opens messages with that hashtag on RHS', () => {
        const testSearch = '/test url test-search';

        // # Post message
        cy.postMessage(testSearch);

        // # Expand the test-search message
        cy.get('#showMoreButton').click().wait(TIMEOUTS.HALF_SEC);

        // # Click the #hello on the test-search message
        cy.get('[data-hashtag="#hello"]').first().click().wait(TIMEOUTS.HALF_SEC);

        // # RHS should be visible with search results
        cy.get('#search-items-container').should('be.visible');

        // * Assert search results are present and correct
        cy.get('[data-testid="search-item-container"]').should('be.visible').should('contain.text', '#hello');
    });
});
